<script setup lang="ts">
import { getLodop } from '@/lib/lodop/LodopFuncs'
import dayjs from 'dayjs'
import printModal from '@/components/business/print-modal/index.vue'
import { ref , nextTick} from 'vue'
import { salesApi } from '@/api/business/sale/sales-api'
import { useUserStore } from '@/store/modules/user-store'
import { useEnums } from '@/hooks/plugins/useEnums'
const userStore = useUserStore()
const userInfo = useUserStore().getUserInfo
const open = ref<boolean>(false)
const { getDescByValue } = useEnums()
const emit = defineEmits(['close', 'getList'])
const enum GoodsType {
  '销' = 1,
  '赠',
  '退',
  '换'
}
const visible = ref(false)
// 详情
const options = ref([])
defineExpose({
  showDrawer
})
function showDrawer(item) {
  options.value = [item]
  visible.value = true
}
function onClose() {
  visible.value = false
}
const loading = ref(false)
const usePrint = () => {
  const printLoading = ref(false)
  const onPrint = async (data?: any[]) => {
    loading.value = true
    const list = data ?? options?.value

for (let i = 0; i < list.length; i++) {
    const valData = await salesApi.detail({ id: list[i].id, is_print: true })
    options.value = [valData.data]
  }

  await nextTick()

    const LODOP = getLodop()
    if (!LODOP) {
      open.value = true
      loading.value = false
      return
    }
    LODOP.PRINT_INITA('0mm', '0mm', '241mm', '93mm', '车销单')
    LODOP.SET_PRINT_PAGESIZE(1, '215.9mm', '93mm', '车销单')
    for (let i = 0; i < list.length; i++) {
      const p_dom = document.getElementById(list[i].id)
      LODOP.ADD_PRINT_TABLE(0, '3%', '94%', '100%', p_dom?.outerHTML)
      LODOP.SET_PRINT_STYLEA(0, 'TableRowThickNess', '12pt')
      LODOP.NewPageA()
      
    }
    LODOP.PREVIEW()
    loading.value = false
  }
  return {
    onPrint,
    printLoading
  }
}
const { onPrint, printLoading } = usePrint()
const handleModalClose = () => {
  open.value = false
}
</script>

<template>
  <printModal :open="open" @cancel="handleModalClose" />
  <a-drawer title="车销单打印" :open="visible" @close="onClose">
    <template #closeIcon>
      <close @on-close="onClose" />
    </template>
    <div class="relative" v-for="(item, index) in options" :key="item.id">
      <a-button class="absolute top-0 right-0 z-2" type="primary" @click="() => onPrint([options[index]])" :loading="printLoading">补单打印</a-button>
      <table :id="item.id" border="0" center cellSpacing="0" cellPadding="1" width="100%" style="border-collapse: collapse; font-size: 11pt">
        <tbody>
        <tr>
     <td colspan="8">
              <div style="line-height: 30px; text-align: center; font-size: 16pt; font-weight: 700">{{ userStore.getCompanyInfo.name }}车销单</div>
              <font style="position: absolute; right: 0; top: 0">
                <span tdata="pageNO" format="#">#</span>
                <span>/</span>
                <span tdata="pageCount" format="#">##</span>
              </font>
            </td>
  </tr>
        </tbody>
        <tbody>
          <tr>
            <td colspan="8">客户名称: {{ item?.store?.store_name || '--' }}&nbsp;&nbsp;&nbsp;&nbsp;出库仓库: {{ item?.warehouse?.name || '--' }}({{ getDescByValue('WAREHOUSE_ATTRIBUTE_TYPE', item?.warehouse?.attribute_type) }})&nbsp;&nbsp;&nbsp;&nbsp;开单业务: {{ item?.salesman?.rename }}&nbsp;&nbsp;&nbsp;&nbsp;订单金额: {{ item?.discounted_amount }}元&nbsp;&nbsp;&nbsp;&nbsp;下单日期: {{ item?.create_time }}&nbsp;&nbsp;&nbsp;&nbsp;打印次数: {{ item?.printLog?.print_count || 0 }}&nbsp;&nbsp;&nbsp;&nbsp;打印日期: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}&nbsp;&nbsp;&nbsp;&nbsp;单据编号: {{ item?.no }} </td>
          </tr>
          <tr>
            <td width="5%" style="border: 1pt solid #000">
              <div align="center"><b>序号</b></div>
            </td>
            <td style="border: 1pt solid #000">
              <div align="center"><b>商品名称</b></div>
            </td>
            <td width="10%" style="border: 1pt solid #000">
              <div align="center"><b>商品条码</b></div>
            </td>
            <td width="5%" style="border: 1pt solid #000">
              <div align="center"><b>单位</b></div>
            </td>
            <td width="7%" style="border: 1pt solid #000">
              <div align="center"><b>数量</b></div>
            </td>
            <td width="7%" style="border: 1pt solid #000">
              <div align="center"><b>单价</b></div>
            </td>
            <td width="7%" style="border: 1pt solid #000">
              <div align="center"><b>小计</b></div>
            </td>
            <td style="border: 1pt solid #000">
              <div align="center"><b>备注</b></div>
            </td>
          </tr>
          <tr v-for="(subItem, subIndex) in item?.detail" :key="subItem.id">
            <td style="border: 1pt solid #000; text-align: center">{{ subIndex + 1 }}</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.goods?.goods_name }}</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.spec?.barcode }}</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.spec?.unit_name }}</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.num }} {{ subItem.spec?.unit_name }}</td>
            <td style="border: 1pt solid #000; text-align: center">{{ Number(subItem.real_price).toFixed(2) }}元</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.total_price }}元</td>
            <td style="border: 1pt solid #000; text-align: center">{{ subItem.remark }}</td>
          </tr>
          <tr>
            <td style="border: 1pt solid #000; text-align: center">总计</td>
            <td style="border: 1pt solid #000">&nbsp;</td>
            <td style="border: 1pt solid #000">&nbsp;</td>
            <td style="border: 1pt solid #000">&nbsp;</td>
            <td style="border: 1pt solid #000; text-align: center">{{ item.detail?.reduce((prev, cur) => prev + cur.num, 0) }}</td>
            <td style="border: 1pt solid #000">&nbsp;</td>
            <td style="border: 1pt solid #000; text-align: center"> {{ item?.detail.reduce((prev, cur) => prev + Number(cur.total_price), 0).toFixed(2) }}元 </td>
            <td style="border: 1pt solid #000">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="8" style="font-weight: 700">制单员:{{ userInfo?.username ?? userInfo.phone }}&nbsp;&nbsp;&nbsp;&nbsp;备注: {{ item?.remark }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <template #footer>
      <div class="flex justify-end">
        <a-button type="default" @click="onClose" style="margin-right: 8px">关闭</a-button>
        <a-button :loading="loading" type="primary" @click="onPrint(options)">打印</a-button>
      </div>
    </template>
  </a-drawer>
</template>
